using System;
using System.IO;
using Doc.Xroad.Eu.Headers;
using Doc.Xroad.Eu.Messages;
using Xtee.Core.Envelope;
using Xtee.Core.Envelope.Header;
using Xtee.XteeAdapter.Doc;

namespace DocumentServingApplication.Services
{
    public class AddDocumentService : AddDocumentServiceHandler
    {
        public AddDocumentResponse ProcessRequest(AddDocumentRequest input, AddDocumentInputHeader header)
        {
            if (string.IsNullOrEmpty(input.Document.Name))
                throw new ApplicationException("Document friendly name is required");

            if (input.Document.Sisu == null || input.Document.Sisu.Directory == null)
                throw new Exception("input.DocumentRef null or input.docref.Directory null");
            string attachmentDir = input.Document.Sisu.Directory.FullName;
            string newname = input.Document.Name;

            if (File.Exists(attachmentDir + "\\" + newname))
                throw new ApplicationException("Document with name : " + input.Document.Name + " Already exists!");

            File.Move(input.Document.Sisu.FullName, attachmentDir + "\\" + newname); //rename 

            return new AddDocumentResponse();

        }
    }


}
